package com.leyou.controller;

import java.util.Arrays;
import java.util.List;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.leyou.api.R;
import com.leyou.common.page.PageResult;
import com.leyou.vo.BrandAndProduct;
import com.leyou.vo.GoodsVo;
import com.leyou.vo.SpuPageParamVo;
import com.leyou.vo.SpuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.leyou.entity.SpuEntity;
import com.leyou.service.SpuService;


/**
 * spu表，该表描述的是一个抽象性的商品，比如 iphone8
 * @author Daifeng
 * @email 2661184062@qq.com
 * @date 2020-12-25 16:44:46
 */
@RestController
@RequestMapping("/spu")
public class SpuController {

    @Autowired
    private SpuService spuService;

    /**
     * 新增商品
     * @param goodsVo
     * @return
     */
    @PostMapping("/SaveGoods")
    public R addGoods(@RequestBody GoodsVo goodsVo){
        spuService.addgoods(goodsVo);
        return  R.ok();
    }
    /**
     * 修改商品信息
     * @param goodsVo
     * @return
     */
    @PutMapping("/SaveGoods")
    public R SaveGoods(@RequestBody GoodsVo goodsVo){
        spuService.SaveGoods(goodsVo);
        return  R.ok();
    }

    // 获取每个品牌销售的商品比例  首页的手机品牌市场
    @GetMapping("/getBrandAndProduct")
    public R getBrandAndProduct(){
        List<BrandAndProduct> list = spuService.getBrandAndProduct();
        return R.ok().put("data",list);
    }



    @GetMapping("/page")
    public R setBrandServic(SpuPageParamVo paramVo) {
        // 添加分页条件
        PageResult<SpuVo> pages =  spuService.getSpuPages(paramVo);
        return  R.ok().put("data",pages);
    }

    /*
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		SpuEntity spu = spuService.getById(id);
        return R.ok().put("spu", spu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SpuEntity spu){
		spuService.save(spu);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SpuEntity spu){
		spuService.updateById(spu);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Long id){
		spuService.deleteProduct(id);
        return R.ok();
    }


    /**
     * 上架/下架
     */
    @GetMapping("/saleable/{id}/{saleable}")
    public R Issaleable(@PathVariable("saleable") Long saleable,@PathVariable("id") Long id){
        spuService.SaveSaleable(saleable,id);
        if(saleable == 1){
            return R.ok("下架成功");
        }else {
            return R.ok("上架成功");
        }
    }
}
